<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="图标" prop="iconUrl">
                <uploadImage dir="functionArea" :limit="1" :fileList="imgList" @onFilesChange="onImgsChange"></uploadImage>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="跳转path">
                <el-input v-model="ruleForm.page"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="是否显示（总控）">
                <el-radio v-model="ruleForm.enable" label="1">是</el-radio>
                <el-radio v-model="ruleForm.enable" label="0">否</el-radio>
            </el-form-item>
            <el-form-item label="小程序端是否显示">
                <el-radio v-model="ruleForm.wechatApplet" label="1">是</el-radio>
                <el-radio v-model="ruleForm.wechatApplet" label="0">否</el-radio>
            </el-form-item>
            <el-form-item label="Android端是否显示">
                <el-radio v-model="ruleForm.android" label="1">是</el-radio>
                <el-radio v-model="ruleForm.android" label="0">否</el-radio>
            </el-form-item>
            <el-form-item label="iOS端是否显示">
                <el-radio v-model="ruleForm.ios" label="1">是</el-radio>
                <el-radio v-model="ruleForm.ios" label="0">否</el-radio>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiFunctionAreaUpdate } from "@/api/configManage.js";
import uploadImage from '@/components/uploadImage.vue'
export default {
    name: "function_area_index_edit",
    components: { uploadImage },
    props: {
        editItem: {
            type: Object,
            required: false
        }
    },
    data() {
        return {
            imgList: [],
            ruleForm: {
                iconUrl: "",
                name: "",
                path: "",
                sort: "",
                enable: "1",
                wechatApplet: "1",
                android: "1",
                ios: "1",
            },
            rules: {
                iconUrl: [{ required: true, message: "请上传图标", trigger: "change" }],
                name: [{ required: true, message: "请输入名称", trigger: "change" }],
                sort: [{ required: true, message: "请输入排序", trigger: "change" }],
            },
            loading: false,
        }
    },
    created() {
        this.setForm()
    },
    watch: {
        editItem() {
            this.setForm()
        }
    },
    methods: {
        setForm() {
            const { id, iconUrl, name, path, sort, enable, wechatApplet, android, ios } = this.editItem
            this.ruleForm = {
                id, iconUrl, name, path, sort,
                enable: enable.toString(),
                wechatApplet: wechatApplet.toString(),
                android: android.toString(),
                ios: ios.toString()
            }
            this.imgList = [{ url: iconUrl }]
        },
        onImgsChange(keyList) {
            this.ruleForm.iconUrl = keyList.join()
        },
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    const postData = this.ruleForm
                    postData.iconUrl = postData.iconUrl.replace("https://oss.paopaofu.com/", "")
                    apiFunctionAreaUpdate(postData).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>